/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Data
{
	import Util.Debug;
	import Util.Utils;
	
	import __AS3__.vec.Vector;
	
	public class ArraySetText extends ArrayView
	{
		public function ArraySetText(iDataArray:IArray, ...fields)
		{
			super(iDataArray);
			
			for(var i:int = 0 ; i < fields.length ; i++)
				AddField(fields[i]);
			
			arrIndicies = null;
				
			Index();
		}
		
		private var arrFields:Array = new Array();
		
		private function AddField(name:String):void
		{
			arrFields.push(name);
		}
		
		private var query:String = "";
		
		public function SetQuery(query:String):void
		{
			//Debug.Trace("Set Query: " +query);
			if( this.query == query ) return;
			
			this.query = query;
			
			if( query.length == 0  )
			{
				arrIndicies = null;
			}
			
			else
			{
				if( arrIndicies == null ) arrIndicies = new Array();
				else arrIndicies.length = 0;
			
				var si:int = Utils.BinaryChop(vec, query, Chop, -1);
				if( si != -1 )
				{
					
					var ei:int = Utils.BinaryChop(vec, query, Chop, +1);
			
					var i:int;
				
					for(i = si ; i <= ei ; i++)
					{
						var x:uint = vec[i];
						var xI:uint	= 0xFFFF & (x >>  0);
						
						if( Boolean(dictIndexIncluded[xI]) == false )
						{ 
							arrIndicies.push(xI);
							dictIndexIncluded[xI] = true;
						}
					}
					
					
					
					for(i = 0 ; i < arrIndicies.length ; i++)
						delete dictIndexIncluded[arrIndicies[i]];
				}
			}
			
			
			if( iArrayListener != null ) iArrayListener.ItemsReplaced();
		}
		
		private var dictIndexIncluded:Object = new Object();
		
		private var vec:Vector.<uint> = new Vector.<uint>();
		
		private function Index():void
		{
			Debug.ProfileStart(this, "textindex");
			
			vec.length = 0;
			
			var c:int = 0;
			var itemLen:int = iDataArray.GetNumItems();
			for(var f:int = 0 ; f < arrFields.length ; f++)
			{
				var field:String = arrFields[f];
				
				for(var i:int = 0 ; i < itemLen; i++)
				{
					var item:Object = iDataArray.GetItem(i);
					
					var str:String = item[field];
					
					var strLen:int = str.length;
					for(var s:int = 0 ; s < strLen ; s++)
					{
						vec[int(c++)] = (uint(s) << 24) | (uint(f) << 16) | (0xFFFF & uint(i));
					}
				}
			}
			
			vec.sort(Cmp);
			
			/*
			for(var i:int = 0 ; i < vec.length ; i++)
			{
				var x:uint = vec[i];
				var xS:uint = 0x00FF & (x >> 24);
				var xF:uint	= 0x00FF & (x >> 16);
				var xI:uint	= 0xFFFF & (x >>  0);
				var xStr:String = String(iDataArray.GetItem(xI)[arrFields[xF]]);
				trace(i + ": " + xStr.substr(xS) + "   Field: " + xF + ", Index=" + xI + ", xS=" + xS);
			}*/
			
			Debug.ProfileStop(this, "textindex");
			Debug.Trace("Text index time: " + Debug.Get("textindex"));
		}
	
		private function Cmp(x:uint, y:uint):Number
		{
			var xS:uint = 0x00FF & (x >> 24);
			var xF:uint	= 0x00FF & (x >> 16);
			var xI:uint	= 0xFFFF & (x >>  0);
			var xStr:String = String(iDataArray.GetItem(xI)[arrFields[xF]]);
			
			var yS:uint = 0x00FF & (y >> 24);
			var yF:uint	= 0x00FF & (y >> 16);
			var yI:uint	= 0xFFFF & (y >>  0);
			var yStr:String = String(iDataArray.GetItem(yI)[arrFields[yF]]);
			
			return Cmp2(xStr, xS, yStr, yS, false);
		}
		
		private function Cmp2(xStr:String, xS:uint, yStr:String, yS:int, ySubStringEqual:Boolean):Number
		{
			var xLen:int = xStr.length - xS;
			var yLen:int = yStr.length - yS;
		
			var len:int = xLen < yLen? xLen : yLen;
			
			for(var i:int = 0 ; i < len ; i++)
			{
				var xC:Number = xStr.charCodeAt(xS++);
				var yC:Number = yStr.charCodeAt(yS++);
				
					 if( xC < yC ) return -1;
				else if( xC > yC ) return  1;
			}
			
			if( ySubStringEqual == false ) return xLen < yLen? -1 : 1;
			else			   			   return xLen < yLen? -1 : 0;			
		}
		
		private function Chop(arr:Vector.<uint>, value:String, index:int):int
		{
			var x:uint = arr[index];
			var xS:uint = 0x00FF & (x >> 24);
			var xF:uint	= 0x00FF & (x >> 16);
			var xI:uint	= 0xFFFF & (x >>  0);
			var xStr:String = String(iDataArray.GetItem(xI)[arrFields[xF]]);
			
			var yStr:String = value;
			var yS:int = 0;
			
			return Cmp2(xStr, xS, yStr, yS, true);
		}
		
		public override function ItemsRearranged():void { throw ""; }
		public override function ItemsReplaced():void { throw ""; }
		public override function ItemInserted(index:int):void {throw "";}
		public override function ItemRemoved(index:int):void {throw "";}
		public override function ItemUpdated(index:int):void {throw "";}
	}
}


